<template>
	<el-dialog
		:title="dialog.title"
		v-model="dialog.dialogVisible"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
		destroy-on-close
		width="1000px"
		:zIndex="999"
		top="0"
	>
		<el-form ref="form" v-loading="formLoading" label-width="120px">
			<el-row>
				<el-col :span="24">
					<el-form-item label="使用说明" prop="remark">
						<sc-editor v-model="remark" :height="400" style="width: 100%;"></sc-editor>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="dialog.dialogVisible = false">取 消</el-button>
				<el-button type="primary" v-if="!btnStatus" disabled>结束后禁止编辑</el-button>
				<el-button type="primary" v-else @click="submitData">提 交</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script>
import {defineAsyncComponent} from "vue";
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	name: "remark",
	components: {
		scEditor
	},
	data() {
		return {
			dialog: {
				dialogVisible: false,
				title: ""
			},
			formLoading: false,
			remark: "",
			btnStatus: false
		};
	},
	methods:{
		closePop() {
			this.dialog.dialogVisible = false;
		},
		getOneData(_id) {
			const that = this;
			this.formLoading = true;
			this.$API.shop.coupon.getRemark({ id: _id }).then((res) => {
				if (res.code === 0) {
					that.remark = res.data;
				}
				else {
					that.$message.error(res.meseage);
				}
				that.formLoading = false;
			});
		},
		open() {
			this.dialog.title = "新增优惠券";
			// 初始化数据
			this.dialog.dialogVisible = true;
		},
		edit(_id, btnStatus = true) {
			this.id = _id;
			this.btnStatus = btnStatus;
			this.dialog.title = "编辑优惠券详情";
			this.dialog.dialogVisible = true;
			this.getOneData(_id);
		},
		submitData() {
			let formData = { id: this.id, remark: this.remark };
			this.formLoading = true;
			this.$API.shop.coupon
				.setRemark(formData)
				.then((res) => {
					this.formLoading = false;
					console.log(res);
					if (res.code === 0) {
						this.$message.success(this.dialog.title + "成功");
						this.closePop();
						this.$emit("success", formData);
					}
					else {
						this.$message.error(res.message);
					}
				})
				.catch((err) => {
					this.formLoading = false;
					this.$message.error(err);
				});
		}
	}
}
</script>

<style scoped>

</style>
